SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[SaveCOIOrganization]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[SaveCOIOrganization]
GO

/***************************************************************************
* Name:			SaveCOIOrganization
* Purpose:		Insert/Update a COIOrganization
*
* PARAMETERS
* Name				Description					
* -------------			-----------------------------------------------------------
* @id				Unique id ( 0 for new records to be inserted)
* @name				Name of the organization
* @description			Description
* @admPersonId			Users Person id
* @active			Indicates whether or not the record is accepted
* @reviewed			Indicates whether or not the record is reviewed
****************************************************************************/
CREATE PROCEDURE [dbo].[SaveCOIOrganization] (

@id INT,
@name VARCHAR(75),
@description VARCHAR(512) = null, 
@admPersonId INT,
@active BIT,
@reviewed BIT

)

AS

BEGIN
	IF @id = 0
	BEGIN


		INSERT INTO COIOrganization( Name,
						Description, 
						AdmPersonId,
						Active,
						Reviewed,
						DateCreated
					)
					VALUES
					( @name,
						@description, 
						@admPersonId,
						@active,
						@reviewed,
						GetDate()
					)
		
		IF @@ERROR <> 0 GOTO ERROR_HANDLER
		
		SELECT IDENT_CURRENT('COIOrganization')

		
		RETURN 
	END
	ELSE
	BEGIN
		
		UPDATE COIOrganization
		SET
			NamE = @name,
			Description = @description, 
			AdmPersonId = @admPersonId,
			Active = @active,
			Reviewed = @reviewed,
			DateModified = GetDate()
		WHERE [Id] = @id
		
		IF @@ERROR <> 0 GOTO ERROR_HANDLER

		SELECT @id	
		RETURN
		
	END

	ERROR_HANDLER:
		IF @@ERROR <> 0 
		BEGIN
			RAISERROR('Unable to Save COIOrganization', 16, 1)
			RETURN
		END
END

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

